import scala.io.StdIn


/**
 * @author: hesion
 * @date: 2021-06-11 12:07
 *        人机猜拳
 *        1.1 作业需求
 *         1. 选取对战角色
 *            2. 开始对战，用户出拳，与对手进行比较，提示胜负信息
 *            3. 猜拳结束算分，平局都加一分，获胜加二分，失败不加分
 *            4. 循环对战，当输入“n”时，终止对战，并显示对战结果
 *            5. 游戏结束后显示得分
 * */
object HomeWork2 {


  def main(args: Array[String]): Unit = {


    // 储存角色的Map
    val RoleMap: Map[String, String] = Map("1" -> "刘备", "2" -> "张飞", "3" -> "刘备")

    // 储存出拳类型的Map
    val punchMap: Map[String, String] = Map("1" -> "剪刀", "2" -> "布", "3" -> "石头")

    //存储对立结果的map
    //赢
    val winMap = Map("剪刀" -> "布", "石头" -> "剪刀", "布" -> "石头")
    //输
    val loseMap = Map("剪刀" -> "石头", "石头" -> "布", "布" -> "剪刀")


    // 接收键盘输入的键
    var key: String = ""
    // 选择的英雄
    var chooseRole: String = ""
    // 分数记录
    var selfScore: Int = 0
    //敌人的分数
    var enemyScore: Int = 0;

    //对战次数
    var count: Int = 0;
    println("**********************************")
    println("***************猜拳开始************")
    println("**********************************")


    println("请选择对战角色: (1.刘备 2.关羽  3.张飞)")
    key = StdIn.readLine()
    chooseRole = RoleMap.getOrElse(key, "1")
    println("你选择的英雄是" + chooseRole)
    println("是否开始?(y/n)")
    key = StdIn.readLine()
    if ("y".equals(key)) {
      println("对战开始！！输入n退出")

      while (!key.equals("n")) {
        count += 1
        println("请你出拳！！1. 剪刀 2. 布  3.石头")
        key = StdIn.readLine()
        println("你出的是" + punchMap.getOrElse(key, "剪刀"))
        //角色出拳
        val roleChoice = scala.util.Random.nextInt(2) + 1
        val punchRoleResult = punchMap.getOrElse(String.valueOf(roleChoice), "未知操作")
        println(chooseRole + "出的是" + punchMap.getOrElse(String.valueOf(roleChoice), "剪刀"))

        //玩家
        val maybeString = winMap(punchMap.getOrElse(key, "剪刀"))

        //双方博弈
        if (key.equals(String.valueOf(roleChoice))) {
          println("结果:平局")
          selfScore += 1
          enemyScore += 1
        } else if (maybeString.equals(punchRoleResult)) {
          println("结果:玩家胜出")
          selfScore += 2
        } else {
          println("结果:" + chooseRole + "胜出")
          enemyScore += 2
        }
      }

      println("退出游戏")
      println("--------------")
      println(chooseRole + " VS 玩家")
      println("对战次数:"+count)
      println("玩家得分:"+selfScore)
      println(chooseRole+"得分:"+enemyScore)
    } else if ("n".equals(key)) {
      println("拜拜！")
    }
  }
}
